<template>
    <detail-page
        :loading="loading"
        :reference="() => $refs"
        :anchors="anchors"
        :title="title"
        :description="headerDescription"
        :extra="headerExtra"
        :buttons="buttons"
        @close="close"
    >
        <abstract-form :model="form" :rules="rules">
            <collapse-card ref="progress" header="流程进度">
                <doc-steps :status="form.status" :finish="form.finish" :type="type"/>
            </collapse-card>

            <collapse-card ref="base" header="发布标题">
                    <el-input  v-model="form.name" :readonly="!canSave" >
                    </el-input>
            </collapse-card>

            <collapse-card ref="introduce" header="团队介绍">
                <el-input
                    v-model="form.introduce"
                    :rows="4"
                    :readonly="!canSave"
                    maxlength="100"
                    show-word-limit
                />
            </collapse-card>

            <collapse-card ref="attachment" header="获奖证书">
                <qiniu-upload
                    :file-list="form.imageList"
                    :disabled="!canSave"
                    @remove="removeUpload"
                    @success="uploadSuccess"
                />
            </collapse-card>

            <collapse-card ref="remark" header="备注">
                <el-input
                    v-model="form.remark"
                    :rows="4"
                    :readonly="!canSave"
                    maxlength="200"
                    show-word-limit
                    type="textarea"
                />
            </collapse-card>
        </abstract-form>

    </detail-page>
</template>

<script>
import docDetailMixin from "@/mixin/docDetailMixin"
import {getById, add, update, commit, pass} from "@/api/doc/honorwall/wall"
import {isEmpty} from "@/util"
import {mul, plus} from "@/util/math"
import {isInteger} from "@/util/validate"

export default {
    name: "honorwall",

    mixins: [docDetailMixin],

    data() {
        this.docName = '荣誉墙数据'
        this.api = {getById, add, update, commit, pass}
        return {
            form: {
                id: null,
                name: null,
                finish: 0,
                ftime: null,
                remark: null,
                introduce: null
            },
            rules: {
                ame: [{required: true, message: '标题不能为空', trigger: 'change'}]
            },
            anchors: [
                {ref: 'progress', label: '发布标题'},
                {ref: 'introduce', label: '团队介绍'},
                {ref: 'attachment', label: '证书'},
                {ref: 'remark', label: '备注'},
            ]
        }
    },

    computed: {
        headerDescription() {
            const f = this.$options.filters.timestamp2Date
            return [
                {label: '创建人：', content: this.form.cname},
                {label: '创建时间：', content: f(this.form.ctime)},
                {label: '审核人：', content: this.form.vname},
                {label: '审核时间：', content: f(this.form.vtime)},
                {label: '完成时间：', content: f(this.form.ftime)}
            ]
        },
        headerExtra() {
            return [
                {title: '单据状态', content: this.getStatus(this.form.status)}
            ]
        },
        selectedCategories() {
            return this.form.data.map(i => i.cid)
        },
        canAddSub() {
            return this.form.data.every(i => !i._editable)
        }
    },

    methods: {
        validate() {
            // if (this.form.data.length <= 0) return '销售商品不能为空'
            if (this.type === 'edit' && isEmpty(this.form.id)) {
                return '属性缺失，请关闭弹窗刷新重试'
            }
            let index = 1
            for (let sub of this.form.data) {
                if (isEmpty(sub.cid, sub.cname)) return `第${index}个商品分类不能为空`
                if (isEmpty(sub.num)) return `第${index}个商品数量不能为空`
                if (isEmpty(sub.price)) return `第${index + 1}个商品单价不能为空`
                if (!isInteger(sub.num) || sub.num < 0) return `第${index}个商品数量有误`
                if (!isInteger(sub.price) || sub.price <= 0) return `第${index}个商品单价有误`
                this.form.total = plus(this.form.total, mul(sub.num, sub.price))
                index++
            }
            return null
        }
    }
}
</script>
